package me.evis.mobile.sms2cal;

import java.util.Date;

import me.evis.mobile.sms2cal.model.CalEvent;
import me.evis.mobile.sms2cal.model.SmsData;
import me.evis.mobile.sms2cal.provider.CalEventContentProvider;
import me.evis.mobile.sms2cal.provider.ParseLogContentProvider;
import me.evis.mobile.sms2cal.provider.ParsedEventContentProvider;
import android.app.Activity;
import android.content.ContentUris;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.text.format.DateUtils;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class ParsedEventDetail extends Activity {
	/**
	 * @see android.app.Activity#onCreate(Bundle)
	 */
	@Override
    protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
        setContentView(R.layout.parsedevent_detail);
        
        Uri data = getIntent().getData();
        if (data != null) {
            Cursor c = managedQuery(data, null, null, null, null);
            if (c.getCount() > 0) {
                c.moveToFirst();
                final CalEvent event = getCalEvent(c);
                Date parsedOn = new Date(c.getLong(c.getColumnIndex(ParsedEventContentProvider.PARSE_DATE)));
                boolean read = (c.getInt(c.getColumnIndex(ParsedEventContentProvider.READ)) > 0);
                int parseLogId = c.getInt(c.getColumnIndex(ParsedEventContentProvider.PARSELOG_ID));
                int targetGcalId = c.getInt(c.getColumnIndex(ParsedEventContentProvider.TARGET_GCAL_ID));
                c.close();
                
                refreshCalButtonsState(targetGcalId, event);
//                getTargetGcalId().addTextChangedListener(new TextWatcher() {
//                    @Override
//                    public void onTextChanged(CharSequence s, int start, int before, int count) {
//                    }
//                    
//                    @Override
//                    public void beforeTextChanged(CharSequence s, int start, int count,
//                            int after) {
//                    }
//                    
//                    @Override
//                    public void afterTextChanged(Editable s) {
//                    }
//                });
//                getTargetGcalId().setText(String.valueOf(targetGcalId));
                getSmsParsedOnText().setText(DateUtils.formatDateTime(this, 
                        parsedOn.getTime(), DateUtils.FORMAT_SHOW_TIME));
                getSmsReceivedOnText().setText(DateUtils.formatDateTime(this, 
                        event.sourceSms.receiveOn.getTime(), DateUtils.FORMAT_SHOW_TIME));
                getSmsFromText().setText(event.sourceSms.from);
                getSmsBodyText().setText(event.sourceSms.messageBody);
                getEventSummaryText().setText(event.summary);
                getEventLocationText().setText(event.location);
                getEventTimeText().setText(DateUtils.formatDateRange(this, 
                        event.dtStart.getTime(), event.dtEnd.getTime(), 
                        DateUtils.FORMAT_SHOW_TIME));
                getEventDecriptionText().setText(event.description);
                
                if (!read) {
                    // Mark as read
                    Uri parseLogUri = ContentUris.withAppendedId(ParseLogContentProvider.CONTENT_URI, parseLogId);
                    ContentValues values = new ContentValues();
                    values.put(ParseLogContentProvider.READ, true);
                    getContentResolver().update(parseLogUri, values, null, null);
                }
            }
        }
	}
	
	private void refreshCalButtonsState(final int targetGcalId, final CalEvent newEvent) {
	    // Init button states
        if (targetGcalId > 0) {
            getAddBtn().setEnabled(false);
            
            getRemoveBtn().setEnabled(true);
            getRemoveBtn().setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    removeCalendarItem(targetGcalId, newEvent);
                }
            });
            
            getViewBtn().setEnabled(true);
            getViewBtn().setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    viewCalendarItem(targetGcalId, newEvent);
                }
            });
            
        } else {
            getAddBtn().setEnabled(true);
            getAddBtn().setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    addCalenderItem(newEvent);
                }
            });
            
            getRemoveBtn().setEnabled(false);
            getViewBtn().setEnabled(false);
        }
	}

	private CalEvent getCalEvent(Cursor c) {
	    SmsData sms = new SmsData();
	    sms.from = c.getString(c.getColumnIndex(ParsedEventContentProvider.SMS_FROM));
	    sms.messageBody = c.getString(c.getColumnIndex(ParsedEventContentProvider.SMS_BODY));
	    sms.receiveOn = new Date(c.getLong(c.getColumnIndex(ParsedEventContentProvider.RECEIVE_DATE)));
	    
	    CalEvent event = new CalEvent();
	    event.id = c.getInt(c.getColumnIndex(ParsedEventContentProvider._ID));
	    event.categories = c.getString(c.getColumnIndex(ParsedEventContentProvider.CATEGORIES));
	    event.classification = c.getString(c.getColumnIndex(ParsedEventContentProvider.CLASSIFICATION));
	    event.description = c.getString(c.getColumnIndex(ParsedEventContentProvider.DESCRIPTION));
	    event.dtEnd = new Date(c.getLong(c.getColumnIndex(ParsedEventContentProvider.DTEND)));
	    event.dtStamp = new Date(c.getLong(c.getColumnIndex(ParsedEventContentProvider.DTSTAMP)));
	    event.dtStart = new Date(c.getLong(c.getColumnIndex(ParsedEventContentProvider.DTSTART)));
	    event.location = c.getString(c.getColumnIndex(ParsedEventContentProvider.LOCATION));
	    event.sourceSms = sms;
	    event.summary = c.getString(c.getColumnIndex(ParsedEventContentProvider.SUMMARY));
	    
	    return event;
	}
	
	private void addCalenderItem(CalEvent event) {
	    int targetGcalId = CalendarUtil.addToCalendar(this, event);
        updateTargetGcalIdInCalEvent(event.id, targetGcalId);
	    refreshCalButtonsState(targetGcalId, event);
	}
	
	private void removeCalendarItem(int calId, CalEvent event) {
	    CalendarUtil.removeFromCalendar(this, calId);
	    updateTargetGcalIdInCalEvent(event.id, 0);
	    refreshCalButtonsState(0, event);
	}
	
	private void viewCalendarItem(int calId, CalEvent event) {
	    CalendarUtil.viewInCalendar(this, calId);
	}
	
	private void updateTargetGcalIdInCalEvent(int calEventId, int gcalId) {
	    Uri calEventUri = ContentUris.withAppendedId(CalEventContentProvider.CONTENT_URI, calEventId);
        ContentValues values = new ContentValues();
        values.put(CalEventContentProvider.TARGET_GCAL_ID, gcalId);
        getContentResolver().update(calEventUri, values, null, null);
	}
	
	private Button getAddBtn() {
	    return (Button) findViewById(R.id.addBtn);
	}
	
	private Button getRemoveBtn() {
	    return (Button) findViewById(R.id.removeBtn);
	}
	
	private Button getViewBtn() {
	    return (Button) findViewById(R.id.viewBtn);
	}
	
	private TextView getTargetGcalId() {
	    return (TextView) findViewById(R.id.targetGcalId);
	}
	
	private TextView getEventDecriptionText() {
	    return (TextView) findViewById(R.id.eventDecriptionText);
	}
	
	private TextView getEventLocationText() {
	    return (TextView) findViewById(R.id.eventLocationText);
	}
	
	private TextView getEventTimeText() {
	    return (TextView) findViewById(R.id.eventTimeText);
	}
	
	private TextView getEventSummaryText() {
	    return (TextView) findViewById(R.id.eventSummaryText);
	}
	
    private TextView getSmsReceivedOnText() {
        return (TextView) findViewById(R.id.smsReceivedOnText);
    }
    
    private TextView getSmsParsedOnText() {
        return (TextView) findViewById(R.id.smsParsedOnText);
    }
    
    private TextView getSmsBodyText() {
        return (TextView) findViewById(R.id.smsBodyText);
    }
    
    private TextView getSmsFromText() {
        return (TextView) findViewById(R.id.smsFromText);
    }
}
